package com.wang.tool.webtool;

import com.wang.tool.common.result.SimpleResult;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

class Demo {

    public static void main(String[] args) {
//        spring常用web工具，1.全局异常/结果处理，2.Date出入参序列化/反序列化
//        1.spring启动类添加@EnableWebTool并配置
//        2.json mapping的响应会经过 处理  see Cont1


    }

//  自定义响应包装器，自定义反序列化日期格式化
    @EnableWebTool(errorBuilder =Bui.class,dateSerializeFormat = WebDateConverterConfig.DATE_TIME_FORMAT)
    private static class Application {

        public static void main(String[] args) {
            SpringApplication.run(Application.class);
        }
    }


    @Controller
    @RequestMapping("/")
    private static class Cont1 {


        @ResponseBody
        @RequestMapping("/r")
        public Date r(Date date) {
            return date;
        }
    }

    private static class Bui implements WebErrorResponseBuilder {


        @Override
        public Object build4InternalError(SimpleResult<Void> defErrorResult, Throwable e, HttpServletRequest request, HttpServletResponse response) {
            return defErrorResult;
        }

        @Override
        public Object build4ErrorURI(SimpleResult<Void> defErrorUriResult, HttpServletResponse response) {
            return defErrorUriResult;
        }
    }


}